package com.xbongbong.pro.rabbitmq.producer.impl;

import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.connector.pojo.dto.ConnectorHistoryDataInitDTO;
import com.xbongbong.pro.connector.pojo.dto.ConnectorRabbitMqDTO;
import com.xbongbong.pro.rabbitmq.binders.ConnectorProducerBinder;
import com.xbongbong.pro.rabbitmq.producer.ConnectorProducer;
import com.xbongbong.saas.enums.OperateTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 连接器生产者
 * 创建时间 2020/12/25 4:51 PM
 * 修改时间 2020/12/25 4:51 PM
 * @author chy
 */
@Service("connectorProducer")
@EnableBinding({ConnectorProducerBinder.class})
public class ConnectorProducerImpl implements ConnectorProducer {
    private static final Logger LOG = LoggerFactory.getLogger(ConnectorProducerImpl.class);
    @Resource
    private ConnectorProducerBinder connectorProducerBinder;

    @Override
    public void sendMessage(ConnectorRabbitMqDTO connectorRabbitMqDTO) throws XbbException {
        LOG.info("connectorProducer sendMessage");
        if (Objects.nonNull(connectorRabbitMqDTO)) {
            MessageHeaderAccessor messageHeaderAccessor = new MessageHeaderAccessor();
            messageHeaderAccessor.setHeader("x-delay", 10000);
            MessageBuilder messageBuilder = MessageBuilder.withPayload(connectorRabbitMqDTO).setHeaders(messageHeaderAccessor);
            connectorProducerBinder.connectorOutput().send(messageBuilder.build());
        }
    }

    @Override
    public void sendMessage(ConnectorHistoryDataInitDTO connectorHistoryDataInitDTO) throws XbbException {
        MessageHeaderAccessor messageHeaderAccessor = new MessageHeaderAccessor();
        MessageBuilder messageBuilder = MessageBuilder.withPayload(connectorHistoryDataInitDTO).setHeaders(messageHeaderAccessor);
        connectorProducerBinder.connectorHistoryDataInitOutput().send(messageBuilder.build());
    }
}
